package com.example.service.impl;

import com.example.entity.Student;
import com.example.entity.example.StudentExample;
import com.example.entity.bo.StudentCourseResultBO;
import com.example.mapper.StudentMapper;
import com.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(readOnly = true)
public class StudentServiceImpl implements StudentService {

    @Autowired(required = false)
    StudentMapper studentMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void add(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public Student getById(String id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateById(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    @Override
    @Transactional
    public void updateSelectiveById(Student student) {
        studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    @Transactional
    public void removeById(String id) {
        studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Student> getByExampleWithLimit(StudentExample example, Integer page, Integer limit) {
        int begin = (page - 1) * limit;
        return studentMapper.selectByExampleWithLimit(example, begin, limit);
    }

    @Override
    public long getCountByExample(StudentExample example) {
        return studentMapper.countByExample(example);
    }

    @Override
    public List<StudentCourseResultBO> getStudentCourseResultsById(String id) {
        return studentMapper.getCourseResultsById(id);
    }

    @Override
    public StudentCourseResultBO getStudentCourseResultsByIdAndCourseId(String studentId, String courseId) {
       return studentMapper.getCourseResultsByIdAndCourseId(studentId,courseId);
    }

    @Override
    public List<Student> getByTeacherId(String teacherId) {
        return studentMapper.selectByTeacherId(teacherId);
    }
}
